package soar.template.springboot.service.hello.controller.hello;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.stream.Collectors;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import soar.template.springboot.service.rmi.hello.CreateAccountRequest;
import soar.template.springboot.service.rmi.hello.CreateAccountResponse;
import soar.template.springboot.service.rmi.hello.Hello;
import soar.template.springboot.service.hello.controller.BaseController;
import soar.template.springboot.service.hello.controller.BaseRequest;
import soar.template.springboot.service.hello.controller.BaseResponse;

@Controller
@RequestMapping("/hello")
@Service("Hello")
public class HelloController extends BaseController implements Hello
{
    private final static Logger logger = LoggerFactory
            .getLogger(HelloController.class);

    // GET 提取全部参数
    @RequestMapping(value = "/testget", method = RequestMethod.GET, produces = {
            "application/json;charset=UTF-8" })
    @ResponseBody
    public TestGETResponse testGET(HttpServletRequest servletRequest)
    {
        SortedMap<String, String> request_params = get_request_params(
                servletRequest);

        TestGETResponse response = new TestGETResponse(0, "成功");
        String request_values = request_params.entrySet().stream()
                .map(x -> x.getKey() + ":" + x.getValue())
                .collect(Collectors.joining(","));
        response.setRequest_values(request_values);
        return response;
    }

    // POST
    /*
     * request_body: {"current_time":"2016-06-15 16:34:11", "id":100,
     * "name":"小米", "addrs":["北京","上海"]}
     */
    @RequestMapping(value = "/testpost", method = RequestMethod.POST, consumes = {
            "application/json;charset=UTF-8" }, produces = {
                    "application/json;charset=UTF-8" })
    @ResponseBody
    public TestPOSTResponse testPOST(@RequestBody TestPOSTRequest request,
            HttpServletRequest servletRequest)
    {
        logger.info("wrapped response body({})",
                get_request_body(servletRequest));
        logger.info("wrapped response body({})",
                get_request_body(servletRequest));

        TestPOSTResponse response = new TestPOSTResponse(0, "成功");
        response.setEcho(String.format(
                "current_time(%s) id(%d) name(%s) addrs(%s)",
                request.getCurrent_time(), request.getId(), request.getName(),
                request.getAddrs().stream().collect(Collectors.joining("|"))));
        return response;
    }

    // RMI接口
    @Resource
    private TestService testService;

    @Override
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    public String test(@RequestParam(defaultValue = "arron") String echo)
    {
        return testService.run(echo);
    }

    @Resource
    private CreateAccountService createAccountService;

    // @RequestMapping(value = "/createaccount", method = RequestMethod.GET)
    // @ResponseBody
    // public CreateAccountResponse createAccount(@RequestParam String login,
    // @RequestParam String name, @RequestParam int age,
    // @RequestParam String address)
    // {
    // CreateAccountRequest request = new CreateAccountRequest();
    // request.setLogin(login);
    // request.setName(name);
    // request.setAge(age);
    // request.setAddress(address);
    // return createAccountService.run(request);
    // }

    // RMI接口
    // 创建用户
    @Override
    @RequestMapping(value = "/createaccount", method = RequestMethod.POST, consumes = {
            "application/json;charset=UTF-8" }, produces = {
                    "application/json;charset=UTF-8" })
    @ResponseBody
    public CreateAccountResponse createAccount(
            @RequestBody CreateAccountRequest request)
    {
        return createAccountService.run(request);
    }

    // 记录日志
    
    // 日志列表
}

class TestGETResponse extends BaseResponse
{
    public TestGETResponse(int error, String errmsg)
    {
        super(error, errmsg);
    }

    private String request_values;

    public String getRequest_values()
    {
        return request_values;
    }

    public void setRequest_values(String request_values)
    {
        this.request_values = request_values;
    }

}

class TestPOSTRequest extends BaseRequest
{
    private int id;
    private String name = "";
    private List<String> addrs = new ArrayList<>();

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public List<String> getAddrs()
    {
        return addrs;
    }

    public void setAddrs(List<String> addrs)
    {
        this.addrs = addrs;
    }

}

class TestPOSTResponse extends BaseResponse
{
    public TestPOSTResponse(int error, String errmsg)
    {
        super(error, errmsg);
    }

    private String echo;

    public String getEcho()
    {
        return echo;
    }

    public void setEcho(String echo)
    {
        this.echo = echo;
    }

}
